<template>
  <div class="customer">

    <el-row class="edit-column">
      <el-button type="primary" icon="el-icon-plus" circle @click="addConsumer"></el-button>

      <!--<el-button type="primary" icon="el-icon-edit" circle></el-button>-->
      <!--<el-button type="success" icon="el-icon-check" circle></el-button>-->
      <!--<el-button type="info" icon="el-icon-message" circle></el-button>-->
      <!--<el-button type="warning" icon="el-icon-star-off" circle></el-button>-->
      <!--<el-button type="danger" icon="el-icon-delete" circle></el-button>-->
    </el-row>

    <el-table :data="customerList" border stripe class="item-table">
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column align="center" prop="id" label="客服ID"></el-table-column>
      <el-table-column align="center" prop="name" label="姓名"></el-table-column>
      <el-table-column align="center" prop="memo" label="备注"></el-table-column>
      <el-table-column align="center" prop="status" label="状态">
        <template scope="scope">
          <el-tag v-if="scope.row.status === 1" size="mini" type="success">在职</el-tag>
          <el-tag v-else-if="scope.row.status === 2" size="mini" type="info">离职</el-tag>
          <el-tag v-else size="mini" type="warning">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template scope="scope">
          <el-button type="text" size="mini" style="color: #1E90FF" @click="updateConsumer(scope.row)">编辑</el-button>
          <el-button type="text" size="mini" style="color: red" @click="deleteConsumer(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 15, 20, 50]"
        :current-page.sync="page.current"
        :page-size="page.size"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>

    <!--新增/更新客服人员框-->
    <el-dialog :title=dialogTitle :visible.sync="consumerDialogVisible" width="40%">
      <el-form ref="form" :model="addConsumerInfo" label-width="80px">
        <el-form-item label="客服ID">
          <el-input v-if="dialogTitle === addConsumerDialogTitle" v-model="addConsumerInfo.id"></el-input>
          <el-input v-if="dialogTitle === updateConsumerDialogTitle" v-model="addConsumerInfo.id" disabled></el-input>
        </el-form-item>

        <el-form-item label="姓名">
          <el-input v-model="addConsumerInfo.name"></el-input>
        </el-form-item>

        <el-form-item label="备注">
          <el-input v-model="addConsumerInfo.memo"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="consumerDialogVisible = false; addConsumerInfo = {}">取 消</el-button>
        <el-button type="primary" @click="clickConsumer">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    components: {},
    name: 'Customer',
    data () {
      return {
        addConsumerDialogTitle: '新增客服人员',
        updateConsumerDialogTitle: '更新客服人员',
        consumerDialogVisible: false,
        addConsumerInfo: {
          id: '',
          name: '',
          status: '',
          memo: ''
        },
        dialogTitle: '',

        customerList: [],
        page: {
          current: 1,
          size: 10,
          total: 0
        }
      }
    },
    methods: {
      /** 页数修改处理 */
      handleSizeChange(val) {
        this.page.current = 1;
        this.page.size = val;
        this.loadCustomerList();
      },
      handleCurrentChange() {
        this.loadCustomerList();
      },

      addConsumer() {
        this.addConsumerInfo = {};
        this.dialogTitle = this.addConsumerDialogTitle;
        this.consumerDialogVisible = true;
      },
      updateConsumer(consumer) {
        this.addConsumerInfo = consumer;
        this.dialogTitle = this.updateConsumerDialogTitle;
        this.consumerDialogVisible = true;
      },

      deleteConsumer(consumer) {
        this.$confirm('此操作将删除客服 ' + consumer.name +  ' , 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.put('/api/customer/delete', {
            serviceId: consumer.id,
            serviceName: consumer.name,
            memo: consumer.memo,
            status: consumer.status
          }, {emulateJSON: true}).then((res) => {
            if (!this.checkResponse(res)) {
              return;
            }

            this.$message.success('删除成功!');
            this.loadCustomerList();
          });

        }).catch(() => { });
      },

      clickConsumer() {
        if (this.dialogTitle === this.addConsumerDialogTitle) {
          this.$http.post("/api/customer", {
            serviceId:     this.addConsumerInfo.id,
            serviceName:   this.addConsumerInfo.name,
            memo:   this.addConsumerInfo.memo
          }, {emulateJSON: true}).then((res) => {
            if (!this.checkResponse(res)) {
              return;
            }

            this.$message.success("添加客服 " + this.addConsumerInfo.name +" 成功");
            this.consumerDialogVisible = false;
            this.addConsumerInfo = {};
            this.loadCustomerList();
          });

        } else if (this.dialogTitle === this.updateConsumerDialogTitle) {
          this.$http.put("/api/customer", {
            serviceId:     this.addConsumerInfo.id,
            serviceName:   this.addConsumerInfo.name,
            status: this.addConsumerInfo.status,
            memo:   this.addConsumerInfo.memo
          }, {emulateJSON: true}).then((res) => {
            if (!this.checkResponse(res)) {
              return;
            }

            this.$message.success("更新客服 " + this.addConsumerInfo.name +" 成功");
            this.consumerDialogVisible = false;
            this.addConsumerInfo = {};
            this.loadCustomerList();
          });
        } else {
          this.$message.error("操作错误");
        }
      },

      // 加载客服人员列表
      loadCustomerList() {
        this.$http.post("/api/customer/page/list", {
          current: this.page.current,
          size: this.page.size
        },{emulateJSON: true}).then((res) => {
          if (!this.checkResponse(res)) {
            return;
          }

          let page = res.body.data;
          this.page.total = page.total;
          this.customerList = page.data;
        }).catch((e) => {
          this.messageException(e);
        });
      },
    },
    created() {
      this.loadCustomerList();
    }
  }
</script>

<style lang="less" scoped>
  .customer {
  }
</style>
